﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SavePhotoalbum')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SavePhotoalbum'
    DROP  Procedure  proc_cm_SavePhotoalbum
END
GO

PRINT 'Creating Procedure proc_cm_SavePhotoalbum'
GO

CREATE Procedure dbo.proc_cm_SavePhotoalbum
	@pPhotoalbumId uniqueidentifier
	,@pHtmlPageId uniqueidentifier = null
	,@pTitleTxt nvarchar(100)
	,@pDescTxt nvarchar(2000) = null
	,@pLatestPhotoId uniqueidentifier = null
	,@pArchiveInd bit
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

	declare @vNow datetime
	
	set @vNow = getdate()
	
	if not exists (select * from [dbo].[tbl_photoalbum] where [photoalbum_id] = @pPhotoalbumId)
	begin
		insert into [dbo].[tbl_photoalbum]
		(
			[photoalbum_id]
			,[html_page_id] 
			,[title_txt]
			,[desc_txt]
			,[create_dttm]
			,[update_dttm]
			,[latest_photo_id]
			,[archive_ind]
		)
		values
		(
			@pPhotoalbumId
			,@pHtmlPageId
			,@pTitleTxt
			,@pDescTxt
			,@vNow
			,@vNow
			,@pLatestPhotoId
			,@pArchiveInd
		)
	end
	else
	begin
		update
			[dbo].[tbl_photoalbum]
		set
			[title_txt] = @pTitleTxt
			,[desc_txt] = @pDescTxt
			,[html_page_id] = @pHtmlPageId
			,[update_dttm] = @vNow
			,[latest_photo_id] = @pLatestPhotoId
			,[archive_ind] = @pArchiveInd
		where
			[photoalbum_id] = @pPhotoalbumId
	end
END
GO

GRANT EXEC ON dbo.proc_cm_SavePhotoalbum TO PUBLIC
GO

